package cc.rengu.igas.channel.cups.core.service.trans;

import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.cups.core.model.CupsFieldRspBean;
import cc.rengu.igas.channel.cups.core.service.base.BasePayService;
import cc.rengu.igas.channel.cups.facade.request.DepositCancelRequest;
import cc.rengu.igas.channel.cups.facade.response.DepositCancelResponse;
import cc.rengu.igas.share.facade.bean.CupsField;
import cc.rengu.igas.share.facade.bean.CupsFieldBean;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.utility.util.AmountUtil;
import cc.rengu.oltp.utility.util.BeanUtil;

/**
 * 功能描述：受理侧存款撤销请求
 *
 * @author wangyongshuai
 * @date 2020/11/23
 */
public class CupsDepositCancelService extends BasePayService<DepositCancelRequest, DepositCancelResponse> {
    @Override
    protected ChnlBizResponse callBizService(CupsFieldBean cupsFieldBean, DepositCancelRequest depositCancelRequest,
                                             DepositCancelResponse depositCancelResponse) throws Exception {
        //  请求  结构体
        CupsField cupsField = cupsFieldBean.getCupsField();

        BeanUtil.beanCopy(depositCancelRequest, cupsField);
        cupsField.setMsgType("0200");
        cupsField.setProcCode("170000");
        cupsField.setConCode("00");
        cupsField.setOrderAmt(AmountUtil.changeY2F(cupsField.getOrderAmt()));

        //  终端读取能力
        //  IC卡条件代码
        //  小额免密标志
        //  终端类型
        cupsFieldBean.createF60()
                .setF60_2_2(depositCancelRequest.getTermRead())
                .setF60_2_3(depositCancelRequest.getIcConCode())
                .setF60_2_5(depositCancelRequest.getTermInfo().getDeviceType());
        cupsFieldBean.build();

        //  调用 CUPS   TODO 此交易配置暂时有误 ,关联了行内交易码, 后续解耦
        CupsFieldRspBean cupsFieldRspBean = callCups(cupsFieldBean, "P03CF001");

        //  应答
        CupsField cupsFieldRsp = cupsFieldRspBean.getCupsFieldRsp();
        BeanUtil.beanCopy(cupsFieldRsp, depositCancelResponse);
        depositCancelResponse.setRespCode(cupsFieldRsp.getRespCode());
        if (depositCancelResponse.getSettleDate() != null) {
            depositCancelResponse.setSettleDate(getCupsSettleDate(depositCancelResponse.getSettleDate()));
        }
        if (CupsParamConstant.SUCCESS_CODE.equals(cupsFieldRsp.getRespCode())) {
            depositCancelResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            depositCancelResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
        }
        return new ChnlBizResponse(depositCancelResponse);
    }

}
